/****************************************************************************\
*                                                                            *
*  Copyright 2008 The CAnt2 Project (http://code.google.com/p/cant2)         *
*                                                                            *
*  Licensed under the Apache License, Version 2.0 (the "License");           *
*  you may not use this file except in compliance with the License.          *
*  You may obtain a copy of the License at                                   *
*                                                                            *
*      http://www.apache.org/licenses/LICENSE-2.0                            *
*                                                                            *
*  Unless required by applicable law or agreed to in writing, software       *
*  distributed under the License is distributed on an "AS IS" BASIS,         *
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
*  See the License for the specific language governing permissions and       *
*  limitations under the License.                                            *
*                                                                            *
\****************************************************************************/

#include "UnitTest.h"
#include "CAnt2/src/core/HashTable.h"

/****************************************************************************/

namespace {

    TEST ( HashTableUsage )
    {
        typedef HashTable<int> HT;
        HT ht;
        unsigned p;

        CHECK_EQUAL ( size_t ( 0 )  , ht.Used()      );
        CHECK_EQUAL ( HT::MinSize   , ht.Capacity()  );
        CHECK_EQUAL ( HT::Invalid   , ht.FindFirstMatching ( 0 )  );

        CHECK_NOTHROW   ( ht.Insert ( 0, 1 ) );

        CHECK_EQUAL     ( size_t ( 1 )  , ht.Used()      );
        CHECK_EQUAL     ( HT::MinSize   , ht.Capacity()  );
        p = ht.FindFirstMatching ( 0 );
        CHECK_NOTEQUAL  ( HT::Invalid   , p  );
        CHECK_EQUAL     ( 1, ht.Get(p) );
        p = ht.FindNextMatching ( p, 0 );
        CHECK_EQUAL     ( HT::Invalid   , p  );

        CHECK_NOTHROW   ( ht.Insert ( 0, 2 ) );

        CHECK_EQUAL     ( size_t ( 2 )  , ht.Used()      );
        CHECK_EQUAL     ( HT::MinSize   , ht.Capacity()  );
        p = ht.FindFirstMatching ( 0 );
        CHECK_NOTEQUAL  ( HT::Invalid   , p  );
        CHECK_EQUAL     ( 1, ht.Get(p) );
        p = ht.FindNextMatching ( p, 0 );
        CHECK_NOTEQUAL  ( HT::Invalid   , p  );
        CHECK_EQUAL     ( 2, ht.Get(p) );

        CHECK_NOTHROW   ( ht.Remove ( ht.FindFirstMatching ( 0 ) ) );

        CHECK_EQUAL     ( size_t ( 1 )  , ht.Used()      );
        CHECK_EQUAL     ( HT::MinSize   , ht.Capacity()  );
        p = ht.FindFirstMatching ( 0 );
        CHECK_NOTEQUAL  ( HT::Invalid   , p  );
        CHECK_EQUAL     ( 2, ht.Get(p) );
        p = ht.FindNextMatching ( p, 0 );
        CHECK_EQUAL     ( HT::Invalid   , p  );

        HT empty;
        ht.swap ( empty );

        CHECK_EQUAL ( size_t ( 0 )  , ht.Used()      );
        CHECK_EQUAL ( HT::MinSize   , ht.Capacity()  );
        CHECK_EQUAL ( HT::Invalid   , ht.FindFirstMatching ( 0 )  );
    }
};

